/**
 * 
 */
package ar.edu.utn.frsf.cidisi.ginet.generation.upcolbpip2cginet.elementstemplate;

import org.eclipse.emf.common.util.EList;
import org.pnml.tools.epnk.pnmlcoremodel.AnnotationGraphics;
import org.pnml.tools.epnk.pnmlcoremodel.Coordinate;
import org.pnml.tools.epnk.pnmlcoremodel.Name;
import org.pnml.tools.epnk.pnmlcoremodel.PnmlcoremodelFactory;
import org.pnml.tools.epnk.pntypes.hlpng.pntd.hlpngdefinition.Declaration;

import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Arc;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Page;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Place;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.RefPlace;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Transition;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.BusinessMessage;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.ControlFlowSegment;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionPath;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionProtocolElement;

/**
 * @author Neri
 *
 */
	
public class IfUpcolbpipStructure extends ChoreographyStructure {
	
	public IfUpcolbpipStructure(Integer i){		
		super();
		this.pos = i;
		//Name		
		AnnotationGraphics graphicsName = PnmlcoremodelFactory.eINSTANCE
				.createAnnotationGraphics();
		
		Name nameLabel = PnmlcoremodelFactory.eINSTANCE.createName();
		
		Coordinate coordinate2 = PnmlcoremodelFactory.eINSTANCE.createCoordinate();
		
		coordinate2.setX(-9);
		coordinate2.setY( -39);
		
		graphicsName.setOffset(coordinate2);
		
		nameLabel.setText("IF");
		nameLabel.setGraphics(graphicsName);
		
		this.abstractPage.setName(nameLabel);
	}
	
	@Override
	public void createPageStructure(InteractionProtocolElement element, Place placeIn, Place placeOut, EList<Declaration> ginetDeclarations, Place termination, EList<BusinessMessage> messages){
	
		this.fillVarDeclAndNameSort(ginetDeclarations);
		
		this.setPageName(element);
		
		EList<InteractionPath> interP = ((ControlFlowSegment)element).getInteractionPath(); 
		
		RefPlace refPlaceIn = this.createRefPlace("InPlace",  1, 2, "InPlace", -4, -4, "1", placeIn);
		
		Transition transitionIt = this.createTransition("ifit", 2, 2, "ifit", -4, -9, "1", "1", null);
		Arc arcPInTit = this.createArc("", this.getVarDec("gi"), -37, -3, refPlaceIn, transitionIt);

		Transition transitionOt = this.createTransition("ifot", 6, 2, "ifot", -9, -4, "1", "1", null);
		
		RefPlace refPlaceOut = this.createRefPlace("OutPlace", 7, 2, "OutPlace", -4, -4, "1", placeOut);

		Arc arcTotPOut = this.createArc("", this.getVarDec("gi"), -9, -19, transitionOt, refPlaceOut);
		
		Place placeIpp = createPlace("ifipp", 3, 2, "ifipp", -4, -4, this.getVarDec("gi"), 6, 21, true, "1");
		
		Place placeOpp = createPlace("ifopp", 5, 2, "ifopp", -4, -4, this.getVarDec("gi"), 6, 21, false, "1");
		
		Arc arcTitPIpp = this.createArc("", this.getVarDec("gi"), -12, -8, transitionIt, placeIpp);
		
		Arc arcPoppTot = this.createArc("", this.getVarDec("gi"), -24, -3, placeOpp, transitionOt);			
		
		for(int i = 1; i <= interP.size() && (i<=2); i++){
			
			Page page = createPage("page" + i,4,i, "page" + i, -9, -39);
			
			this.createInteractionProtocol(interP.get(i-1), i, page, placeIpp, placeOpp, ginetDeclarations, termination, messages);
			
			this.abstractPage.getObject().add(page);
		}
		
		this.abstractPage.getObject().add(placeIpp);
		this.abstractPage.getObject().add(placeOpp);
		
		this.abstractPage.getObject().add(refPlaceIn);
		this.abstractPage.getObject().add(refPlaceOut);
		
		this.abstractPage.getObject().add(transitionIt);
		this.abstractPage.getObject().add(transitionOt);
		
		this.abstractPage.getObject().add(arcPInTit);
		this.abstractPage.getObject().add(arcTitPIpp);		
		this.abstractPage.getObject().add(arcPoppTot);
		this.abstractPage.getObject().add(arcTotPOut);		
	}
}